<?php

namespace App\Amqp\Consumer;

use App\Cache\InviteVideoCache;
use App\Cache\RedisKey;
use App\Constants\InviteVideoCode;
use App\Helpers\RedisUtil;
use App\Model\CloseFriend;
use App\Model\InviteVideoLog;
use App\Model\PayOrder;
use App\Model\UserBlacklist;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Service\ServiceTrait\GetUserInfoTrait;
use App\Service\ServiceTrait\InviteVideoCheckTrait;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Message\Type;
use Hyperf\Amqp\Result;
use PhpAmqpLib\Message\AMQPMessage;
use Hyperf\Amqp\Annotation\Consumer;

/**
 * @Consumer(exchange="inviteVideo", routingKey="inviteVideo", queue="inviteVideo.send", name ="InviteVideoConsumer", nums=1)
 */
class InviteVideoConsumer extends ConsumerMessage
{
    use GetUserInfoTrait,InviteVideoCheckTrait;

    /**
     * @var string
     */
    protected $type = Type::DIRECT;

    public function consumeMessage($data, AMQPMessage $message): string
    {
        if (!$data['user_id']) {
            log_error('InviteVideoConsumer:error:NoUserIdOrGender:data:' . json_encode($data));
            return Result::ACK;
        }

        $userId = $data['user_id'];

        $profile = $this->getUserInfo($userId);
        $gender = $profile->gender;

        //女生达到标准踢出
        if ($gender == 0 && InviteVideoLog::getFemaleInviteVideoRefuseCountToday($userId) >= InviteVideoCode::TODAY_MAX_ANSWER_NUM_FEMALE){
            log_info('InviteVideoConsumer:info:MaxOverrun:userId:' . $userId);
            return Result::ACK;
        }

        //男用户不符合条件踢出
        $payList = array_values(array_unique(PayOrder::getIsPayAll()));
        if ($gender == 1 && !$this->checkMaleStatus($userId,$payList)){
            InviteVideoCache::delUserInviteList($userId);
            log_info('InviteVideoConsumer:info:UserDisqualify:userId:' . $userId);
            return Result::ACK;
        }


        $redis = RedisUtil::getRedis();

        if (InviteVideoCache::isLock($userId,$gender)){
            log_info('InviteVideoConsumer:info:UserIsLock:userId:' . $userId);
            return Result::ACK;
        }

        //判断有没有参加邀约视频
        $joinKey = RedisKey::joinInviteVideoToday();
        if (!$redis->sIsMember($joinKey,$userId)){
            log_info('InviteVideoConsumer:info:NoJoinInviteVideo:userId:' . $userId);
            return Result::ACK;
        }

        //判断有没有今天的数据
        $key = RedisKey::todayInviteVideoList($userId);
        $inviteList = $redis->sMembers($key);
        if (empty($data)){
            log_info('InviteVideoConsumer:info:NoInviteListData:userId:' . $userId);
            return Result::ACK;
        }

        //去除已经锁定的用户
        if ($gender == 1){
            $lockKey = RedisKey::inviteVideoFemaleLock();
        }else{
            $lockKey = RedisKey::inviteVideoMaleLock();
        }
        $lockUser = $redis->sMembers($lockKey);
        if (!empty($lockUser)){
            $lockUser = array_diff($lockUser,[0]);
        }else{
            $lockUser = [];
        }

        $canInviteList = array_diff($inviteList,$lockUser);
        if (empty($canInviteList)){
            log_info('InviteVideoConsumer:info:NoCanInviteListData:userId:' . $userId);
            return Result::ACK;
        }

        //做个在线用户筛选
        $onlineUserList = UserProfile::getOnlineUserAll();
        $dataList = array_intersect($canInviteList,$onlineUserList);

        //获取密友
        $closeUserList = CloseFriend::getFrdList($userId);

        $beBlackList = UserBlacklist::getBeBlackList($userId);

        $isMale = false;
        if ($gender == 0){
            $isMale = true;
        }

        //打乱数组
        shuffle($dataList);
        
        //发送逻辑
        foreach ($dataList as $item) {
            //密友踢出
            if (in_array($item,$closeUserList)){
                InviteVideoCache::delInviteRefuse($item,$userId);
                continue;
            }

            //拉黑踢出
            if (in_array($item,$beBlackList)){
                InviteVideoCache::delInviteRefuse($item,$userId);
                continue;
            }


            $itemProfile = $this->checkUserCallStatus($item);
            if (!$itemProfile){
                continue;
            }

            if ($isMale && !$this->checkMaleStatus($item,$payList)){
                InviteVideoCache::delInviteRefuse($item,$userId);
                InviteVideoCache::delUserInviteList($item);
                continue;
            }

            if ($isMale){
                $this->sendInviteTip($userId,$itemProfile);
            }else{
                $this->sendInviteTip($item,$profile);
            }

            break;
        }

        return Result::ACK;
    }
}